package com.gqj.bas.service.impl;

import com.gqj.bas.domain.BasWarehouse;
import com.gqj.bas.mapper.BasWarehouseMapper;
import com.gqj.bas.service.IBasWarehouseService;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 仓库Service业务层处理
 *
 * @author lxq
 * @date 2023-12-17
 */
@Service
public class BasWarehouseServiceImpl implements IBasWarehouseService {
    @Autowired
    private BasWarehouseMapper basWarehouseMapper;

    /**
     * 查询仓库
     *
     * @param warehouseId 仓库主键
     * @return 仓库
     */
    @Override
    public BasWarehouse selectBasWarehouseByWarehouseId(Long warehouseId) {
        return basWarehouseMapper.selectBasWarehouseByWarehouseId(warehouseId);
    }

    /**
     * 查询仓库列表
     *
     * @param basWarehouse 仓库
     * @return 仓库
     */
    @Override
    public List<BasWarehouse> selectBasWarehouseList(BasWarehouse basWarehouse) {
        return basWarehouseMapper.selectBasWarehouseList(basWarehouse);
    }

    /**
     * 查询用户下拉框
     *
     * @param basWarehouse 仓库
     * @return 仓库集合
     */
    @Override
    public List<Map<String, Object>> userOptions(BasWarehouse basWarehouse) {
        return basWarehouseMapper.userOptions(basWarehouse);
    }

    ;

    /**
     * 新增仓库
     *
     * @param basWarehouse 仓库
     * @return 结果
     */
    @Override
    public int insertBasWarehouse(BasWarehouse basWarehouse) {
        basWarehouse.setCreateTime(DateUtils.getNowDate());
        return basWarehouseMapper.insertBasWarehouse(basWarehouse);
    }

    /**
     * 修改仓库
     *
     * @param basWarehouse 仓库
     * @return 结果
     */
    @Override
    public int updateBasWarehouse(BasWarehouse basWarehouse) {
        basWarehouse.setUpdateTime(DateUtils.getNowDate());
        return basWarehouseMapper.updateBasWarehouse(basWarehouse);
    }

    /**
     * 批量删除仓库
     *
     * @param warehouseIds 需要删除的仓库主键
     * @return 结果
     */
    @Override
    public int deleteBasWarehouseByWarehouseIds(Long[] warehouseIds) {
        return basWarehouseMapper.deleteBasWarehouseByWarehouseIds(warehouseIds);
    }

    /**
     * 删除仓库信息
     *
     * @param warehouseId 仓库主键
     * @return 结果
     */
    @Override
    public int deleteBasWarehouseByWarehouseId(Long warehouseId) {
        return basWarehouseMapper.deleteBasWarehouseByWarehouseId(warehouseId);
    }

    /**
     * 校验库房信息是否唯一
     *
     * @param basWarehouse 库房信息
     * @return 结果
     */
    @Override
    public String checkWarehouseNameUnique(BasWarehouse basWarehouse) {
        Long warehouseId = StringUtils.isNull(basWarehouse.getWarehouseId()) ? -1L : basWarehouse.getWarehouseId();
        BasWarehouse info = basWarehouseMapper.checkWarehouseNameUnique(basWarehouse.getWarehouseName());
        if (StringUtils.isNotNull(info) && info.getWarehouseId().longValue() != warehouseId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询库房是否被引用
     *
     * @param warehouseId 库房ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkMaterialExistWarehouse(Long warehouseId) {
        int result = basWarehouseMapper.checkMaterialExistWarehouse(warehouseId);
        return result > 0;
    }

    /**
     * 查询库房是否被引用
     *
     * @param warehouseId 库房ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkDeviceExistWarehouse(Long warehouseId) {
        int result = basWarehouseMapper.checkDeviceExistWarehouse(warehouseId);
        return result > 0;
    }

    /**
     * 查询仓库下拉框
     *
     * @param basWarehouse 仓库
     * @return 仓库集合
     */
    @Override
    public List<Map<String, Object>> warehouseOptions(BasWarehouse basWarehouse) {
        return basWarehouseMapper.warehouseOptions(basWarehouse);
    }

}
